package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class SecondActivity2 extends AppCompatActivity {
    AnimationDrawable wodeanim;
    private Animation rotate;
    Button idFan_on,idFan_off,idAC_on,idAC_off,idlight_on,idlight_off,bt_back,bt_2;
    private ImageView img_fan,img_ac,img_light;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jiemian);
        rotate = AnimationUtils.loadAnimation(SecondActivity2.this,R.anim.anim_fan_rotate);
        img_fan =findViewById(R.id.img_fan);
        idFan_on=findViewById(R.id.idFan_on);
        idFan_off=findViewById(R.id.idFan_off);
        img_ac = findViewById(R.id.img_ac);
        idAC_on=findViewById(R.id.idAc_on);
        idAC_off=findViewById(R.id.idAc_off);
        img_light=findViewById(R.id.img_light);
        idlight_on=findViewById(R.id.idLight_on);
        idlight_off=findViewById(R.id.idLight_off);
        View.OnClickListener listen;
        listen =view ->{
            if(view.getId()==R.id.idFan_on){
                img_fan.setAnimation(rotate);
                img_fan.startAnimation(rotate);
                Toast.makeText(SecondActivity2.this,"通风系统已经打开了哦！",Toast.LENGTH_LONG).show();
            }
            else if (view.getId()==R.id.idFan_off) {
                img_fan.setAnimation(rotate);
                img_fan.clearAnimation();
                Toast.makeText(SecondActivity2.this,"通风系统已经关闭了哦！",Toast.LENGTH_LONG).show();
            }
        };
        idFan_on.setOnClickListener(listen);
        idFan_off.setOnClickListener(listen);

        idAC_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_ac.setImageResource(R.drawable.frame_ac);
                wodeanim=(AnimationDrawable) img_ac.getDrawable();
                wodeanim.start();
                Toast.makeText(SecondActivity2.this,"空调已开启了哦！",Toast.LENGTH_LONG).show();
            }
        });
        idAC_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wodeanim.isRunning())
                {
                    wodeanim.stop();
                    img_ac.setImageResource(R.drawable.ac);
                    Toast.makeText(SecondActivity2.this,"空调已经关闭了哦！",Toast.LENGTH_LONG).show();
                }
            }
        });
        idlight_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               img_light.setImageResource(R.drawable.light_on);
               Toast.makeText(SecondActivity2.this,"灯已经打开了哦！",Toast.LENGTH_LONG).show();
            }
        });
        idlight_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_light.setImageResource(R.drawable.light_off);
                Toast.makeText(SecondActivity2.this,"灯已经关闭了哦！",Toast.LENGTH_LONG).show();
            }
        });
        bt_back =findViewById(R.id.button);
        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity2.this, MainActivity.class);
                startActivity(intent);
            }
        });
        bt_2=findViewById(R.id.button2);
        bt_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity2.this, ThirdActivity3.class);
                startActivity(intent);
            }
        });
    }
}
